﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;

public partial class Supplier_Location_Default : System.Web.UI.Page
{
    private SupplierDataContext supplierContext = new SupplierDataContext();

    private int SupplierId
    {
        get { return Helper.QSIntValue("sid"); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.SupplierLocationDefault;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        if (!IsPostBack)
        {
            LoadSuppliers();
            LoadSupplierLocations();
            ddlSuppliers.Visible = UserAccess.IsCurrentUser(Enums.UserTypes.Admin);
        }
    }

    private void LoadSupplierLocations()
    {
        gvSupplierLocations.DataSource = from sl in supplierContext.SupplierLocations
                                         join s in supplierContext.Suppliers on sl.SupplierId equals s.SupplierId
                                         join l in supplierContext.Locations on sl.LocationId equals l.LocationId
                                         join r in supplierContext.Regions on l.RegionId equals r.RegionId
                                         where sl.SupplierId == Convert.ToInt32(ddlSuppliers.SelectedValue)
                                         select new
                                         {
                                             sl.SupplierLocationId,
                                             s.SupplierName,
                                             l.LocationName,
                                             r.RegionName,
                                             sl.City,
                                             sl.Phone,
                                             sl.Mobile,
                                             Active = (sl.Active.Value ? "Yes" : "No")
                                         };
        gvSupplierLocations.DataBind();

        hpyAddNew.NavigateUrl = "addedit.aspx?sid=" + ddlSuppliers.SelectedValue;
    }

    private void LoadSuppliers()
    {
        ddlSuppliers.DataSource = supplierContext.Suppliers;
        ddlSuppliers.DataTextField = "SupplierName";
        ddlSuppliers.DataValueField = "SupplierId";
        ddlSuppliers.DataBind();

        ddlSuppliers.SelectedValue = SupplierId.ToString();
    }

    protected void ddlSuppliers_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadSupplierLocations();
    }

    protected void gvSupplierLocations_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Helper.ApplyDeleteConfirmMsg(e, 7);
        }
    }

    protected void gvSupplierLocations_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

    protected void gvSupplierLocations_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvSupplierLocations.DataKeys[e.RowIndex].Value);
        var obj = supplierContext.SupplierLocations.SingleOrDefault(u => u.SupplierLocationId == id);

        var locationTimes = from slt in supplierContext.SupplierLocationTimes
                            where slt.SupplierLocationId == id
                            select slt;

        supplierContext.SupplierLocationTimes.DeleteAllOnSubmit(locationTimes);
        supplierContext.SupplierLocations.DeleteOnSubmit(obj);

        supplierContext.SubmitChanges();

        Response.Redirect("default.aspx");
    }
    protected void gvSupplierLocations_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "LocationVehicles")
        {
            string id = gvSupplierLocations.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            Response.Redirect("Vehicles.aspx?slid=" + id + "&sid=" + ddlSuppliers.SelectedValue);
        }
    }
}
